import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

/**
 * The start of the wizard-mode process.
 * This is the first screen that prompts the user with
 * general settings, such as font size and paper type.
 * 
 * @author James Stanier
 */
public class WizardGUI implements ActionListener {
	
	String outputFileName;
	String outputDir;
	String paperType;
	int fontSize;
	boolean twoSidedPrinting;
	
	JFrame prevWindow;
	JFrame window;
	JComboBox fontSelect;
	JComboBox paperSelect;
	JButton nextButton;
	JButton backButton;
	JCheckBox twoSidedButton;
	
	ArrayList<File> inputFiles;

	/**
	 * Constructor.
	 * @param j The previous window.
	 * @param f The list of input files.
	 * @param s The output directory.
	 * @param t The output filename.
	 */
	public WizardGUI(JFrame j, ArrayList<File> f, String s, String t) {
		fontSize = 10;
		twoSidedPrinting = false;
		paperType = "a4paper";
		outputFileName = t;
		outputDir = s;
		prevWindow = j;
		inputFiles = f;
		wizardScreen();
	}
	
	/**
	 * Configures and draws the screen.
	 */
	public void wizardScreen() {
		window = new JFrame("JavaLiN - Wizard mode");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,500);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);
		
		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);
		
		// Heading information
		JPanel progress = new JPanel();
		JPanel header = new JPanel();
		progress.setLayout(new BorderLayout());
		header.setLayout(new BorderLayout());
		JLabel backwards = new JLabel("<HTML><font size=2>&#171; File output settings</HTML>");
		progress.add(backwards, BorderLayout.WEST);
		JLabel forwards = new JLabel("<HTML><font size=2> In-depth commenting &#187;</HTML>");
		progress.add(forwards, BorderLayout.EAST);
		JLabel heading = new JLabel("<HTML><b><font size=5>1. General settings</b></HTML>");
		heading.setHorizontalAlignment(JLabel.CENTER);
		heading.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		header.add(heading, BorderLayout.CENTER);
		JLabel description = new JLabel("<HTML><font size=3>Please enter your preferred font size, output paper size" +
				" and two-sided printing preference before we begin.</HTML>");
		description.setBorder(BorderFactory.createEmptyBorder(20,10,10,10));
		header.add(description, BorderLayout.SOUTH);
		top.add(progress, BorderLayout.CENTER);
		top.add(header, BorderLayout.SOUTH);
		
		// Options in centre
		JPanel options = new JPanel();
		options.setLayout(new GridLayout(2,0));
		
		// Combo boxes
		JPanel comboBoxPanel = new JPanel();
		comboBoxPanel.setLayout(new GridLayout(2,2));
		comboBoxPanel.setBorder(BorderFactory.createEmptyBorder(10,20,20,10));
		
		JLabel fontLabel = new JLabel("<HTML>Font size</HTML>");
		comboBoxPanel.add(fontLabel);
		String[] fontSizes = {"10pt","11pt","12pt"};
		fontSelect = new JComboBox(fontSizes);
		fontSelect.addActionListener(this);
		fontSelect.setToolTipText("The font size to be used.");
		comboBoxPanel.add(fontSelect);
		
		JLabel paperLabel = new JLabel("<HTML>Paper type</HTML>");
		comboBoxPanel.add(paperLabel);
		String[] paperTypes = {"A4 paper","A5 paper","B5 Paper","Letter paper","Legal paper"};
		paperSelect = new JComboBox(paperTypes);
		paperSelect.addActionListener(this);
		paperSelect.setToolTipText("The type of paper to be used. If the report is not " +
				"to be printed, use A4 as default.");
		
		comboBoxPanel.add(paperSelect);
		
		options.add(comboBoxPanel);
		
		// Two sided check box
		JPanel twosidePanel = new JPanel();
		twosidePanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,10));
		twoSidedButton = new JCheckBox("Two-sided printing");
		twoSidedButton.addActionListener(this);
		twoSidedButton.setToolTipText("Allow two-sided printing.");
		twosidePanel.add(twoSidedButton);
		
		options.add(twosidePanel);
		
		// Navigation buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Next >>");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);
		
		window.add("North",top);
		window.add("West",options);
		window.add("South",navButtons);
		window.validate();
		window.setVisible(true);
	}
	
	/**
	 * Actions to be taken upon component interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(paperSelect)) {
			String temp = (String)paperSelect.getSelectedItem();
			if(temp.equals("A4 paper")) {
				paperType = "a4paper";
			}
			else if(temp.equals("A5 paper")) {
				paperType = "a5paper";
			}
			else if(temp.equals("B5 paper")) {
				paperType = "b5paper";
			}
			else if(temp.equals("Letter paper")) {
				paperType = "letterpaper";
			}
			else if(temp.equals("Legal paper")) {
				paperType = "legalpaper";
			}
		}
		else if(x.equals(fontSelect)) {
			String temp = (String)fontSelect.getSelectedItem();
			if(temp.equals("10pt")) {
				fontSize = 10;
			}
			else if(temp.equals("11pt")) {
				fontSize = 11;
			}
			else if(temp.equals("12pt")) {
				fontSize = 12;
			}
		}
		else if(x.equals(twoSidedButton)) {
			twoSidedPrinting = twoSidedButton.isSelected();
		}
		else if(x.equals(backButton)) {
			clearSettings();
			prevWindow.setVisible(true);
			window.setVisible(false);
		}
		else if(x.equals(nextButton)) {
			window.setVisible(false);
			InDepthGUI idgui = new InDepthGUI(this);
		}
	}
	
	/**
	 * Clear settings relevant to this screen.
	 */
	public void clearSettings() {
		fontSize = 10;
		paperType = "a4paper";
		twoSidedPrinting = false;
	}
	
	/**
	 * Get the current font size.
	 * @return The font size as an integer.
	 */
	public int getFontSize() {
		return fontSize;
	}
	
	/**
	 * Get the current paper type.
	 * @return The paper type as a String.
	 */
	public String getPaperType() {
		return paperType;
	}
	
	/**
	 * Get current two-sided printing status.
	 * @return Whether two-sided printing is enabled.
	 */
	public boolean isTwoSidedPrinting() {
		return twoSidedPrinting;
	}
	
	/**
	 * Get the current window.
	 * @return The current JFrame window.
	 */
	public JFrame getWindow() {
		return window;
	}
	
	/**
	 * Get the current output directory.
	 * @return The output directory as a String.
	 */
	public String getOutputDir() {
		return outputDir;
	}
	
	/**
	 * Get the output file name.
	 * @return The output file name as a String.
	 */
	public String getOutputFileName() {
		return outputFileName;
	}
	
	/**
	 * Get the input source file references.
	 * @return Input source files as File objects.
	 */
	public ArrayList<File> getInputFiles() {
		return inputFiles;
	}
}
